package com.app.ytong.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.ytong.R;

/**
 * 
 * @author Android将军
 *
 */
public class HintEditText extends RelativeLayout implements TextWatcher{
	

	private EditText mEditText;
	private TextView mTextView;
	private int maxLength=100;
	private RelativeLayout mRelativeLayout;
	@SuppressLint("NewApi") public HintEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
	
		
	}
	public HintEditText(Context context) {
		super(context);
		
		
	}
	public HintEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		mRelativeLayout=(RelativeLayout)LayoutInflater.from(context).inflate(R.layout.cutom_edit_view, this,true);
		mEditText=(EditText)mRelativeLayout.findViewById(R.id.edit);
		mTextView=(TextView)mRelativeLayout.findViewById(R.id.text);
//		mTextView.setHint("还可输入"+maxLength+"字");
		mTextView.setHint("0/"+maxLength);
		//限定最多可输入多少字符
		mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)}); 
		mEditText.addTextChangedListener(this);
		
	}
	public void initUI(Context context)
	{
		
		RelativeLayout mRelativeLayout=(RelativeLayout)LayoutInflater.from(context).inflate(R.layout.cutom_edit_view, this,true);
		
		mEditText=(EditText)mRelativeLayout.findViewById(R.id.edit);
		
		 mTextView=(TextView)mRelativeLayout.findViewById(R.id.text);
		
		
	}

	public void setMaxEditLength(int length){
		maxLength = length;
		mTextView.setHint("0/"+maxLength);
		//限定最多可输入多少字符
		mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
	}
	
	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
//		mTextView.setHint("还可输入"+(maxLength-s.toString().length())+"字");
//		mTextView.setHint((maxLength-s.toString().length())+"/"+maxLength);
		mTextView.setHint((s.toString().length())+"/"+maxLength);

	}
	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		
	}
	

	public EditText getEditText(){
		return  mEditText;
	}
}
